package com.li.controller;

import com.li.entities.Customer;
import com.li.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Random;

@Tag(name ="客户Customer接口")
@RestController
@Slf4j
//http:localhost:7777/swagger-ui
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Operation(summary = "数据库初始化2条Customer数据插入")
    @RequestMapping(value = "/customer/add",method = RequestMethod.POST)
    public void addCustomer(){
        for (int i = 0; i < 2; i++){
            Customer customer = new Customer();
            customer.setCname("customer"+i);
            customer.setAge(new Random().nextInt(30)+1);
            customer.setPhone("1234567890"+i);
            customer.setSex((byte)new Random().nextInt(2));
            //这种写法在操作系统更有通用性
            customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
            customerService.addCustomer(customer);
        }
    }
    @Operation(summary = "单个customer查询操作,按照customerId查询")
    @RequestMapping(value = "/customer/{customerId}",method = RequestMethod.GET)
    public Customer findCustomerById(@PathVariable Integer customerId){
        return customerService.findCustomerById(customerId);
    }
    @Operation(summary = "bloomFilter过滤,按照customerId查询")
    @RequestMapping(value = "/customerbloomfilter/{customerId}",method = RequestMethod.GET)
    public Customer findCustomerByIdWithBloomFilter(@PathVariable Integer customerId){
        return customerService.findCustomerByIdWithBloomFilter(customerId);
    }

}
